package praktikumid.w09.p4.interface_example;

import java.util.ArrayList;
import java.util.List;

/**
 * Some examples about classes, interfaces.
 * @author Ago
 *
 */
public class Main {

	/**
	 * the main.
	 * @param args arguments.
	 */
	public static void main(String[] args) {
		// we cannot instantiate abstract class
		//Animal a = new Animal();
		// we can have more general type for the
		// variable.
		Animal a = new Fish();
		Cat c = new Cat();
		
		// list of animals
		// Every element in the list can be type of Animal
		// or it's subclass (as pointed out, Animal itself
		// cannot be instantiated).
		List<Animal> animals = new ArrayList<Animal>();
		
		// let's add some animals to the list
		animals.add(new Cat());
		animals.add(new Fish());
		animals.add(new Frog());
		
		// let's loop through the list
		for (Animal an : animals) {
			if (an instanceof Fish) {
				// if concrete animal in the list
				// is fish
				
				// as the list contains Animal
				// type of references,
				// we have to cast "an"
				// to fish. The object
				// in memory will remain
				// the same, only the reference
				// type is changed.
				Fish f = (Fish) an;
				f.swim();
				// casting and calling a method
				// on the same line
				((Fish) an).swim();
				System.out.println("fish");
			}
			if (an instanceof IWaterAnimal) {
				// we can also check against interface.
				// Here, whether an animal is water animal.
				System.out.println("water animal");
			}
		}

	}

}
